'use strict';

var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');
var _interopRequireWildcard = require('@babel/runtime/helpers/interopRequireWildcard');
Object.defineProperty(exports, '__esModule', {
  value: true,
});
exports.default = void 0;
var _objectSpread2 = _interopRequireDefault(require('@babel/runtime/helpers/objectSpread2'));
var _slicedToArray2 = _interopRequireDefault(require('@babel/runtime/helpers/slicedToArray'));
var _objectWithoutProperties2 = _interopRequireDefault(
  require('@babel/runtime/helpers/objectWithoutProperties'),
);
var _react = _interopRequireWildcard(require('react'));
var _reactDnd = require('react-dnd');
var _excluded = ['index', 'moveRow', 'className', 'style'];
var type = 'DraggableBodyRow';
var DraggableBodyRow = function DraggableBodyRow(_ref) {
  var index = _ref.index,
    moveRow = _ref.moveRow,
    className = _ref.className,
    style = _ref.style,
    restProps = (0, _objectWithoutProperties2.default)(_ref, _excluded);
  var ref = (0, _react.useRef)();
  var _useDrop = (0, _reactDnd.useDrop)({
      accept: type,
      collect: function collect(monitor) {
        // @ts-ignore
        var _ref2 = monitor.getItem() || {},
          dragIndex = _ref2.index;
        if (dragIndex === index) {
          return {};
        }
        return {
          isOver: monitor.isOver(),
          dropClassName: dragIndex < index ? ' drop-over-downward' : ' drop-over-upward',
        };
      },
      drop: function drop(item) {
        // @ts-ignore
        moveRow(item.index, index);
      },
    }),
    _useDrop2 = (0, _slicedToArray2.default)(_useDrop, 2),
    _useDrop2$ = _useDrop2[0],
    isOver = _useDrop2$.isOver,
    dropClassName = _useDrop2$.dropClassName,
    drop = _useDrop2[1];
  var _useDrag = (0, _reactDnd.useDrag)({
      type: type,
      item: {
        index: index,
      },
      collect: function collect(monitor) {
        return {
          isDragging: monitor.isDragging(),
        };
      },
    }),
    _useDrag2 = (0, _slicedToArray2.default)(_useDrag, 2),
    drag = _useDrag2[1];
  drop(drag(ref));
  return /*#__PURE__*/ _react.default.createElement(
    'tr',
    (0, _objectSpread2.default)(
      {
        // @ts-ignore
        ref: ref,
        className: ''.concat(className).concat(isOver ? dropClassName : ''),
        style: (0, _objectSpread2.default)(
          {
            cursor: 'move',
          },
          style,
        ),
      },
      restProps,
    ),
  );
};
var _default = DraggableBodyRow;
exports.default = _default;
